<?php
  require_once("../includes.php");
  $membership = New Membership();
  $membership->confirm_admin();

  $action = $_POST['action'];

  function add_to_time_table($time)
  {
    $time   = trim($time);
    $length = strlen($time);

    if ($length > 4)
    {
      $tmp = explode('-', $time);

      $hour_1 = explode(':', $tmp[0]);
      $hour_2 = explode(':', $tmp[1]);

      if ($hour_1[0] < 10 && !first_character_is_zero($tmp[0]))
      {
        if ($hour_2[0] < 10 && !first_character_is_zero($tmp[1]))
           mysql_query('INSERT INTO lestijden(tijd, type) VALUES("0'.$tmp[0].'-0'.$tmp[1].'", "spinning")');
        else
           mysql_query('INSERT INTO lestijden(tijd, type) VALUES("0'.$tmp[0].'-'.$tmp[1].'", "spinning")');
      }
      else
        mysql_query('INSERT INTO lestijden(tijd, type) VALUES("'.$time.'", "spinning")');
    }
    else
    {
      mysql_query('INSERT INTO lestijden(tijd, type) VALUES("0'.$time.'", "spinning")');
    }
  }

  function update_time_table($time, $id)
  {
    $time   = trim($time);
    $length = strlen($time);

    if ($length > 4)
    {
      $tmp = explode('-', $time);

      $hour_1 = explode(':', $tmp[0]);
      $hour_2 = explode(':', $tmp[1]);

      if ($hour_1[0] < 10 && !first_character_is_zero($tmp[0]))
      {
        if ($hour_2[0] < 10 && !first_character_is_zero($tmp[1]))
           mysql_query('UPDATE lestijden SET tijd="0'.$tmp[0].'-0'.$tmp[1].'" WHERE ID="'.$id.'"');
        else
           mysql_query('UPDATE lestijden SET tijd="0'.$tmp[0].'-'.$tmp[1].'" WHERE ID="'.$id.'"');
      }
      else
        mysql_query('UPDATE lestijden SET tijd="'.$time.'" WHERE ID="'.$id.'"');
    }
    else
    {
      mysql_query('UPDATE lestijden SET tijd="0'.$time.'" WHERE ID="'.$id.'"');
    }
  }

  function remove_from_time_table($id)
  {
    mysql_query('DELETE FROM lestijden WHERE ID="'.$id.'"');
  }

  function first_character_is_zero($time)
  {
    return (substr($time, 0, 1) == '0');
  }

  switch ($action)
  {
    case 'add'  : add_to_time_table($_POST['vanaf']);               break;
    case 'edit' : update_time_table($_POST['vanaf'], $_POST['id']); break;
    case 'del'  : remove_from_time_table($_POST['id']);             break;
  }

  $url = BASE_URL;
  header("Location: $url/tijden/");
?>